//
//  TFVoteListCell.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFVoteListCell: UITableViewCell {
   
    @objc public var tf_width:CGFloat = 0
    @objc public var tf_height:CGFloat = 0
    @objc public  var bgView:UIView!
    @objc public var nameLab:UILabel!
    @objc public var tipsLab:UILabel!
   
    //投票图片
    @objc public var picImg:UIImageView!
    @objc public var model:TFVoteData?
    {
        didSet
        {
            if ((model) != nil)
            {
                self.setModelUI(model: model!)
            }
        }
    }
    var maskLayer:CAShapeLayer!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.bgView = UIView.init()
   
        self.bgView.backgroundColor = UIColor(red: 243/255.0, green: 244/255.0, blue: 245/255.0, alpha: 1.0)
        
        self.bgView.layer.cornerRadius = 4;
        self.bgView.layer.masksToBounds = true;
        self.contentView.addSubview(self.bgView)
        
        self.nameLab = UILabel.init()
        self.nameLab.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
        self.nameLab.font = UIFont.PingFang(size: 16)
        self.nameLab.numberOfLines = 0;
        self.nameLab.lineBreakMode = .byCharWrapping
        self.bgView.addSubview(self.nameLab)
        
        
        self.tipsLab = UILabel.init()
        self.tipsLab.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        self.tipsLab.font = UIFont.PingFang(size: 13)
        self.tipsLab.backgroundColor = UIColor(red: 225/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
        self.tipsLab.textAlignment = .center
        self.bgView.addSubview(self.tipsLab)
        
        self.picImg = UIImageView.init()
        self.picImg.isHidden = true
        self.bgView.addSubview(self.picImg)
 
    }
    
    func getMaskLayer(_ rect:CGRect) {
        
           if (self.maskLayer == nil) {
               self.maskLayer = CAShapeLayer.init()
               
               let maskPath = UIBezierPath.init(roundedRect: rect, byRoundingCorners: .bottomLeft, cornerRadii: CGSize(width: 4.0, height: 4.0))
          
               self.maskLayer.frame = rect;
               self.maskLayer.path = maskPath.cgPath
              
           }
          
    }

    func setModelUI(model:TFVoteData) {
        self.bgView.frame = CGRect(x: 12, y: 0, width: self.tf_width-24, height: self.tf_height-8)
       
        self.tipsLab.frame = CGRect(x: self.bgView.frame.size.width-50, y: 0, width: 50, height: 26)
        self.getMaskLayer(self.tipsLab.bounds)

        self.tipsLab.layer.mask = self.maskLayer;
        self.picImg.frame = CGRect(x: 12, y: (self.bgView.frame.size.height-20)/2.0, width: 27, height: 20)
    
        if model.info.endTime == "0" {
            if (model.voted) {
                self.tipsLab.text = "已投票";
                self.tipsLab.textColor = UIColor(red: 12/255.0, green: 187/255.0, blue: 64/255.0, alpha: 1.0)
                
                self.tipsLab.backgroundColor = UIColor(red: 221/255.0, green: 250/255.0, blue: 239/255.0, alpha: 1.0)
            } else {
                self.tipsLab.text = "未投票";
                self.tipsLab.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                self.tipsLab.backgroundColor = UIColor(red: 225/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
            }
        } else {
            self.tipsLab.text = "已结束";
            self.tipsLab.textColor = UIColor(red: 137/255.0, green: 156/255.0, blue: 175/255.0, alpha: 1.0)
            
            self.tipsLab.backgroundColor = UIColor(red: 226/255.0, green: 233/255.0, blue: 242/255.0, alpha: 1.0)
          
        }
       
        if ((model.info.imageUrl.count) > 0) {
            self.picImg.isHidden = false
            self.nameLab.frame = CGRect(x: self.picImg.frame.maxX+10, y: 23, width: self.bgView.frame.size.width-12-67, height: 45)
            
            self.nameLab.text = "[图片投票]";
            let url:URL? = URL.init(string: model.info.imageUrl as String)
            self.picImg.sd_setImage(with: url, completed: nil)
         
     
        } else {
            self.picImg.isHidden = true
            self.nameLab.frame = CGRect(x: 12, y: 23, width: self.bgView.frame.size.width-12-67, height: 45)
         
            if ((model.info.label.count)) > 0 {
                self.nameLab.text = model.info.label
            } else {
                self.nameLab.text = model.info.title
            }

        }
    }

    
    required init?(coder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
